home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / initargs.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  3KB  |  126 lines

  1. #include <stdio.h>
  2. #include <osbind.h>
  3. #include <string.h>
  4. #include <basepage.h>
  5. #include <errno.h>
  6.  
  7. typedef struct
  8.     {
  9.     char        xarg_magic[4];    /* verification value "xArg" */
  10.     int        xargc;        /* argc */
  11.     char        **xargv;    /* argv */
  12.     char        *xiovector;    /* i/o handle status */
  13.     BASEPAGE    *xparent;    /* pointer to parent's basepage */
  14.     }
  15.     XARG;
  16.  
  17. extern int      _argc;
  18. extern char     **_argv;
  19.  
  20. static char     xmagic[] = "xArg";
  21. static char     hex[] = "0123456789ABCDEF";
  22.  
  23. static char *_sbrk(size)
  24.     register int size;
  25. /*
  26.  * Internal error checking interface for the sbrk() function
  27.  */
  28.     {
  29.     register char *p;
  30.     char *sbrk();
  31.  
  32.     size = (size + 1) & ~1;
  33.     if(p = sbrk(size))
  34.         return(p);
  35.     Cconws("Too many arguments\n");
  36.     _exit(ENSMEM);
  37.     }
  38.     
  39. /*
  40.  * Retrieve extended arguments, if possible, and set up argc and argv[].
  41.  */
  42. void _initargs(cmdline, cmdlen)
  43.     char *cmdline;
  44.     int cmdlen;
  45.     {
  46.     register XARG *xp;
  47.     register char *p, **q;
  48.     register int i, n;
  49.     register long a;
  50.     char *getenv();
  51.  
  52.     if(p = getenv(xmagic))
  53.         {
  54.         /*
  55.          * if the "xArg" variable exists, decode the address
  56.          * and assume that it points somewhere reasonable,
  57.          * though possibly not to a valid XARG struct
  58.          */
  59.         for(a = 0L; *p; ++p)    /* convert ascii-hex to long */
  60.             a = ((a << 4) | (0xF & strpos(hex, *p)));
  61.         xp = ((XARG *) a);
  62.         }
  63.     if((p == NULL)                    /* no extended args */
  64.     || (strncmp(xp->xarg_magic, xmagic, 4))        /* not XARG struct */
  65.     || (xp->xparent != _base->p_parent))        /* not right parent */
  66.         {
  67.         /* copy the command line */
  68.         i = cmdlen;
  69.         p = strncpy(_sbrk(i + 1), cmdline, i);
  70.         p[i] = '\0';
  71.         _argv = q = (char **) _sbrk(sizeof(char *));
  72.         *q = "";                /* argv[0] == "" */
  73.         n = 1;
  74.         /*
  75.          * parse command line image based on whitespace
  76.          */
  77.         if(p = strtok(p, " \t"))
  78.             {
  79.             do
  80.                 {
  81.                 q = (char **) _sbrk(sizeof(char *));
  82.                 ++n;
  83.                 *q = p;
  84.                 }
  85.                 while(p = strtok(NULL, " \t"));
  86.             }
  87.         q = (char **) _sbrk(sizeof(char *));
  88.         *q = NULL;                /* tie off argv */
  89.         _argc = n;
  90.         }
  91.     else                        /* EXTENDED ARGS! */
  92.         {
  93.         /*
  94.          * extended args are easy... just remember to copy the
  95.          * data, since it resides in your parent's data space
  96.          */
  97.         _argc = n = xp->xargc;            /* copy argc */
  98.         i = ((n + 1) * sizeof(char *));
  99.         _argv = q = ((char **) _sbrk(i));
  100.         memcpy(q, xp->xargv, i);        /* copy argv */
  101.         q[n] = NULL;
  102.         do                    /* copy arguments */
  103.             {
  104.             p = _sbrk(strlen(*q) + 1);
  105.             *q = strcpy(p, *q);
  106.             }
  107.             while(*++q);
  108.         }
  109.     if((_argv[0] == NULL) || (_argv[0][0] == '\0'))
  110.         {
  111.         /*
  112.          * argv[0] not set, extract value from parent's dta
  113.          */
  114.         p = (char *) _base->p_parent;    /* get parent's basepage */
  115.         if(p == NULL)
  116.             _argv[0] = "";        /* for sid... */
  117.         else
  118.             {
  119.             p = *(char **)(p+0x7C);    /* get parent's saved usp */
  120.             p = *(char **)(p+0x36);    /* get Pexec'd filename */
  121.             _argv[0] = _sbrk(strlen(p) + 1);
  122.             strcpy(_argv[0], p);    /* copy filename */
  123.             }
  124.         }
  125.     }
  126.